<!DOCTYPE html>
<html lang="cn-zh">

<head>
  
  <meta charset="utf-8">



<meta name="viewport" content="width=device-width, initial-scale=1.0">


  
    <meta name="description" content=" if #available(iOS 14, *) { print(&#34;This code only runs on iOS 14 and up&#34;) } else { print(&#34;This code only runs on iOS 13 and lower&#34;) } 在 guard 语句中使用 available 属性：
guard #available(iOS 14, *) else { print(&#34;Returning if iOS 13 or lower&#34;) return } @available和#available之间的区别 @available: 用于标记类或方法的可用性
#available: 用于仅针对特定平台或版本执行一段代码
@available(iOS 15, *) class App {} @available(iOS 15, *) func app() {} if #available(iOS 15, *) { } ">
  


<meta name="color-scheme" content="light dark">







<meta name="generator" content="Hugo 0.111.2">

<meta content="" name="keywords">
<meta content="swift 使用 @available 或 #available - 通通不同-博客" property="og:title">
<meta content="开发的乐趣在于相通而不同 - " property="og:description">

<meta name="msvalidate.01" content="F56ED94B7E86941E3DACEF208F07D329" />

  <title>
    
    swift 使用 @available 或 #available
     - 通通不同-博客
    
</title>

  <link rel="canonical" href="https://codebeautiful.gitee.io/post/swift_available/">


  








  
    
  
  
  <link rel="stylesheet" href="/css/base.min.4f231885947d13c748c815d93da49f07ab89afb67629f9970b7c054610e88e0b.css" integrity="sha256-TyMYhZR9E8dIyBXZPaSfB6uJr7Z2KfmXC3wFRhDojgs=" crossorigin="anonymous">



</head>

<body>
  <nav class="u-background">
  <div class="u-wrapper">
    <ul class="Banner">
      <li class="Banner-item Banner-item--title">
        <h1 class="Banner-heading">
          <a class="Banner-link u-clickable" href="/">通通不同-博客</a>
        </h1>
      </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/post/">全部</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/categories/">分类</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/tags/">标签</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/about/">关于</a>
        </li>
      
    </ul>
  </div>
</nav>

  <main>
    <div class="u-wrapper">
      <div class="u-padding">
        

  <article>
    <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/swift_available/" rel="bookmark">swift 使用 @available 或 #available</a>
  </h2>
  
    <time datetime="2023-03-30T17:06:01&#43;08:00">30 March, 2023</time>
  
</header>

    
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#75715e">#available</span>(<span style="color:#75715e">iOS</span> <span style="color:#ae81ff">14</span>, <span style="color:#f92672">*</span>) {
</span></span><span style="display:flex;"><span>  print(<span style="color:#e6db74">&#34;This code only runs on iOS 14 and up&#34;</span>)
</span></span><span style="display:flex;"><span>} <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>  print(<span style="color:#e6db74">&#34;This code only runs on iOS 13 and lower&#34;</span>)
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
<p>在 guard 语句中使用 available 属性：</p>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span><span style="color:#66d9ef">guard</span> <span style="color:#75715e">#available</span>(<span style="color:#75715e">iOS</span> <span style="color:#ae81ff">14</span>, <span style="color:#f92672">*</span>) <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>  print(<span style="color:#e6db74">&#34;Returning if iOS 13 or lower&#34;</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#66d9ef">return</span>
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
<h2 id="available和available之间的区别">
  <a class="Heading-link u-clickable" href="/post/swift_available/#available%e5%92%8cavailable%e4%b9%8b%e9%97%b4%e7%9a%84%e5%8c%ba%e5%88%ab">@available和#available之间的区别</a>
</h2>
<p>@available: 用于标记类或方法的可用性</p>
<p>#available: 用于仅针对特定平台或版本执行一段代码</p>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span>@available(iOS <span style="color:#ae81ff">15</span>, <span style="color:#f92672">*</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">class</span> <span style="color:#a6e22e">App</span> {}
</span></span><span style="display:flex;"><span>@available(iOS <span style="color:#ae81ff">15</span>, <span style="color:#f92672">*</span>)
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">app</span>() {}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> <span style="color:#75715e">#available</span>(<span style="color:#75715e">iOS</span> <span style="color:#ae81ff">15</span>, <span style="color:#f92672">*</span>) {
</span></span><span style="display:flex;"><span>  
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>

    


  

  

  





    
  

  </article>


      </div>
    </div>
  </main>
  
  <footer class="Footer">
    <div class="u-wrapper">
      <div class="u-padding">
        开发的乐趣在于相通而不同
      </div>
    </div>
  </footer>


  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d08a2ad355cb7a065a41be081b43297e";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>

</body>

</html>
